package com.robotics

import groovy.json.JsonSlurper
import com.nirima.jenkins.plugins.docker.DockerCloud
import com.nirima.jenkins.plugins.docker.DockerTemplate

class BuildUtils {

    // 根据编译id,从环境变量中获取参数
    def buildProject(String build_id) {
        println "Building project: ${build_id}"
    // 这里可以添加实际的构建逻辑
    }

    def getBuildParam(Map params) {
        def build_id = params.build_id
        println "build_id: ${build_id}"
        def jsonString = params.env.paramString
        // return build_id
        println "jsonString: ${jsonString}"
        def jsonSlurper = new JsonSlurper()
        // // 将字符串解析为 JSON 对象
        def jsonObject = jsonSlurper.parseText(jsonString)
        // println "jsonObject: ${jsonObject}"
        return jsonObject[build_id]
    }
    // 获取节点标签
    def getNodeLabels() {
        def nodeLabels = []
        for (cloud in Jenkins.instance.clouds) {
            for (template in cloud.templates) {
                nodeLabels.add(template.labelString)
            }
        }
        for (node in Jenkins.instance.nodes) {
            nodeLabels.add(node.displayName)
        }

        println "${nodeLabels}"
    }

}
